package org.example.test.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.example.test.dal.entity.Cart;
import org.example.test.dal.entity.CartItem;
import org.example.test.dal.entity.User;
import org.example.test.dal.mapper.CartItemMapper;
import org.example.test.dal.mapper.CartMapper;
import org.example.test.dal.mapper.UserMapper;
import org.example.test.request.CartItemRequest;
import org.example.test.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CartServiceImpl implements CartService {
    private static final Logger LOGGER = LogManager.getLogger(CartServiceImpl.class);

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private CartItemMapper cartItemMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Cart getCart(Long userId) {
        try {
            Cart cart = cartMapper.selectByUserId(userId);
            if (cart == null) {
                throw new RuntimeException("Cart not found");
            }
            return cart;
        } catch (Exception e) {
            LOGGER.error("getCart exception", e);
            throw new RuntimeException("Error getting cart: " + e.getMessage());
        }
    }

    @Override
    public void addItemToCart(Long userName, CartItemRequest cartItemRequest) {
        try {
            User user = userMapper.selectByUserName(userName);
            Cart cart = cartMapper.selectByUserId(user.getId());
            if (cart == null) {
                throw new RuntimeException("Cart not found");
            }
            CartItem cartItem = new CartItem();
            cartItem.setImagePath(cartItemRequest.getImagePath());
            cartItem.setName(cartItemRequest.getProductName());
            cartItem.setCartId(cart.getId());
            cartItem.setPrice(cartItemRequest.getPrice());
            cartItem.setProductId(cartItemRequest.getProductId());
            cartItem.setQuantity(cartItemRequest.getQuantity());
            cartItemMapper.insert(cartItem);
        } catch (Exception e) {
            LOGGER.error("addItemToCart exception", e);
            throw new RuntimeException("Error adding item to cart: " + e.getMessage());
        }
    }

    @Override
    public void removeItemsFromCart(Long userId, List<Long> productIds) {
        try {
            // 查找用户的购物车
            User user = userMapper.selectByUserName(userId);
            Cart cart = cartMapper.selectByUserId(user.getId());
            if (cart == null) {
                throw new RuntimeException("Cart not found for user ID: " + userId);
            }
            // 删除指定的商品项
            for (Long productId : productIds) {
                cartItemMapper.deleteByCartIdAndProductId(cart.getId(), productId);
            }
        } catch (Exception e) {
            LOGGER.error("removeItemsFromCart exception", e);
            throw new RuntimeException("Error removing items from cart: " + e.getMessage());
        }
    }

    @Override
    public List<CartItem> getCartItems(Long userId) {
        try {
            User user = userMapper.selectByUserName(userId);
            Cart cart = cartMapper.selectByUserId(user.getId());
            List<CartItem> cartItems = cartItemMapper.selectByCartId(cart.getId());

            if (cartItems == null) {
                throw new RuntimeException("cartItem not found");
            }
            return cartItems;
        } catch (Exception e) {
            LOGGER.error("getCartItems exception", e);
            throw new RuntimeException("Error getting cart items: " + e.getMessage());
        }
    }

    public void clearCartItems(Long userId, List<Long> productIds) {
        try {
            Cart cart = cartMapper.selectByUserId(userId);
            if (cart == null) {
                throw new RuntimeException("Cart not found");
            }
            cartItemMapper.deleteByCartIdAndProductIds(cart.getId(), productIds);
        } catch (Exception e) {
            LOGGER.error("clearCartItems exception", e);
            throw new RuntimeException("Error clearing cart items: " + e.getMessage());
        }
    }


}
